/*
 ********************************************************************************
 *                      桂林聚联科技有限公司
 *                           Copyright
 *
 *  文件描述 ： 解析算法模块的配置文件，并更新相应的变量
 *
 *
 *  文件名称 ： parseOtdrAlgoConfig.c
 *  创建者   ： 彭怀敏
 *  创建日期 ： 2016-12-13 11:29:31
 *  当前版本 ： 
 ********************************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include "iniparser.h"

char *config_string_translate(const char *src, char *translated)
{
    int i, j, n = strlen(src);
    char c;

    for(i = 0, j = 0; i < n; i++){
        c = src[i];
        if((c == '{') || (c == '}') || (c == '[') || (c == ']') || isspace(c))  continue;
        translated[j++] = c;
    }
    translated[j] = 0;
    return translated;
}

int config_string2flttable(const char *src, float *value)
{
    int i = 0;
    char *p, buf[128];

    config_string_translate(src, buf);
    for(p = strtok(buf, ","); p != NULL; p = strtok(NULL, ",")){
        value[i++] = strtof(p, NULL);
    }
    return i;
}

int config_string2inttable(const char *src, int *value)
{
    int i = 0;
    char *p, buf[128];

    config_string_translate(src, buf);
    for(p = strtok(buf, ","); p != NULL; p = strtok(NULL, ",")){
        value[i++] = strtol(p, NULL, 10);
    }
    return i;
}

int config_string2hextable(const char *src, int *value)
{
    int i = 0;
    char *p, buf[128];

    config_string_translate(src, buf);
    for(p = strtok(buf, ","); p != NULL; p = strtok(NULL, ",")){
        value[i++] = strtol(p, NULL, 16);
    }
    return i;
}

int getOneFltArray(dictionary *ini, char *key, float *value)
{
    char *str;

    str = iniparser_getstring(ini, key, "KEY_NOT_FOUND");
    if(!strcmp(str, "KEY_NOT_FOUND"))  return 0;
    return config_string2flttable(str, value);
}

int getOneIntArray(dictionary *ini, char *key, int *value)
{
    char *str;

    str = iniparser_getstring(ini, key, "KEY_NOT_FOUND");
    if(!strcmp(str, "KEY_NOT_FOUND"))  return 0;
    return config_string2inttable(str, value);
}

int getOneHexArray(dictionary *ini, char *key, int *value)
{
    char *str;

    str = iniparser_getstring(ini, key, "KEY_NOT_FOUND");
    if(!strcmp(str, "KEY_NOT_FOUND"))  return 0;
    return config_string2hextable(str, value);
}

static int getOtdrDefaultPulseWidth_AUTO(dictionary *ini, unsigned int OtdrDefaultPulseWidth_AUTO[8][6])
{
    char key[128];
    char *ml[8] = {"300m", "1km", "5km", "10km", "30km", "60km", "100km", "180km"};
    int i;

    for(i = 0; i < 8; i++){
        sprintf(key, "OtdrDefaultPulseWidth_AUTO:apw%s", ml[i]);
        getOneIntArray(ini, key, OtdrDefaultPulseWidth_AUTO[i]);
    }
    return 1;
}

static int getOtdrAutoMeasureParam(dictionary *ini, unsigned int OtdrAutoMeasureParam[4][3])
{
    char key[128];
    char *k[4] = {"amp_ml", "amp_pw", "amp_rcv", "amp_pow"};
    int i;

    for(i = 0; i < 4; i++){
        sprintf(key, "OtdrAutoMeasureParam:%s", k[i]);
        if(i == 2)  getOneHexArray(ini, key, OtdrAutoMeasureParam[i]);
        else        getOneIntArray(ini, key, OtdrAutoMeasureParam[i]);
    }
    return 1;
}

static int getOtdrPowerLevel(dictionary *ini, unsigned int OtdrPowerLevel[8][13], int lambda)
{
    char key[128];
    char *ml[8] = {"300m", "1km", "5km", "10km", "30km", "60km", "100km", "180km"};
    int i;

    for(i = 0; i < 8; i++){
        sprintf(key, "OtdrPowerLevel%4d:power%4d_%s", lambda, lambda, ml[i]);
        getOneIntArray(ini, key, OtdrPowerLevel[i]);
    }
    return 1;
}

static int getOtdrPowerLevel_LP(dictionary *ini, unsigned int OtdrPowerLevel_LP[2][6], int lambda)
{
    char key[128];
    char *ml[2] = {"100km", "180km"};
    int i;

    for(i = 0; i < 2; i++){
        sprintf(key, "OtdrPowerLevel%4dLP:power%4dlp_%s", lambda, lambda, ml[i]);
        getOneIntArray(ini, key, OtdrPowerLevel_LP[i]);
    }
    return 1;
}

static int getOtdrReceiver(dictionary *ini, unsigned int OtdrReceiver[8][13])
{
    char key[128];
    char *ml[8] = {"300m", "1km", "5km", "10km", "30km", "60km", "100km", "180km"};
    int i;

    for(i = 0; i < 8; i++){
        sprintf(key, "OtdrReceiver:receiver_%s", ml[i]);
        getOneHexArray(ini, key, OtdrReceiver[i]);
    }
    return 1;
}

static int getOtdrReceiver_LP(dictionary *ini, unsigned int OtdrReceiver_LP[2][6])
{
    char key[128];
    char *ml[2] = {"100km", "180km"};
    int i;

    for(i = 0; i < 2; i++){
        sprintf(key, "OtdrReceiverLP:receiverlp_%s", ml[i]);
        getOneHexArray(ini, key, OtdrReceiver_LP[i]);
    }
    return 1;
}

int configOtdrAlgo(char *iniConfigFile, int DataNum)
{
    extern unsigned int *OtdrSampleRate;
    extern unsigned int *OtdrSampleRateHz;
	extern unsigned int OtdrDefaultPulseWidth_AUTO[8][6];
	extern unsigned int OtdrAutoMeasureParam[4][3];
	extern unsigned int OtdrStartPoint[8];
	extern unsigned int OtdrPowerLevel1310[8][13];
	extern unsigned int OtdrPowerLevel1310LP[2][6];
	extern unsigned int OtdrPowerLevel1550[8][13];
	extern unsigned int OtdrPowerLevel1550LP[2][6];
	extern unsigned int OtdrPowerLevel1625[8][13];
	extern unsigned int OtdrPowerLevel1625LP[2][6];
	extern unsigned int OtdrReceiver[8][13];
	extern unsigned int OtdrReceiverLP[2][6];
    extern unsigned int OtdrSampleRate_16K[8];
    extern unsigned int OtdrSampleRate_32K[8];
    extern unsigned int OtdrSampleRateHz_16K[8];
    extern unsigned int OtdrSampleRateHz_32K[8];

    static int OtdrAlgoConfig = 0;

    // check if it has beed configured
    if(OtdrAlgoConfig == 1)     return 0;

    dictionary * ini = iniparser_load(iniConfigFile);
    if(ini == NULL){
        printf("open config file failed\n");
        return -1;
    }
    // print parse result
    //iniparser_dump(ini, stdout);

    getOneIntArray(ini, "OtdrStartPoint:osp", OtdrStartPoint);
    //getOneIntArray(ini, "SampleNum_SampleRate:OtdrSampleRate", OtdrSampleRate);
    //getOneIntArray(ini, "SampleNum_SampleRate:OtdrSampleRateHz", OtdrSampleRateHz);
    if(DataNum == 16000){
        OtdrSampleRate = OtdrSampleRate_16K;
        OtdrSampleRateHz = OtdrSampleRateHz_16K;
    }
    else{
        OtdrSampleRate = OtdrSampleRate_32K;
        OtdrSampleRateHz = OtdrSampleRateHz_32K;
    }

    getOtdrDefaultPulseWidth_AUTO(ini, OtdrDefaultPulseWidth_AUTO);
    getOtdrAutoMeasureParam(ini, OtdrAutoMeasureParam);

    getOtdrPowerLevel(ini, OtdrPowerLevel1310, 1310);
    getOtdrPowerLevel_LP(ini, OtdrPowerLevel1310LP, 1310);

    getOtdrPowerLevel(ini, OtdrPowerLevel1550, 1550);
    getOtdrPowerLevel_LP(ini, OtdrPowerLevel1550LP, 1550);

    getOtdrPowerLevel(ini, OtdrPowerLevel1625, 1625);
    getOtdrPowerLevel_LP(ini, OtdrPowerLevel1625LP, 1625);

    getOtdrReceiver(ini, OtdrReceiver);
    getOtdrReceiver_LP(ini, OtdrReceiverLP);

    iniparser_freedict(ini);

    OtdrAlgoConfig = 1;
    return 0 ;
}

/*
 ********************************************************************************
 *    End  of  File
 ********************************************************************************
*/

